﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Web.Configuration;
using System.Data;
public partial class RaoVat : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["user"]==null)
        {
            Response.Redirect("chuadangnhap.aspx");
           
            
        }

    }
    public void Selected(object sender, EventArgs e)
    {

    }
    public void load(object sender, EventArgs e)
    {
        string sql = "select mashop,tenshop from shop";
        field.DataSource = Database.GetData(sql);
        field.DataBind();
    }
    public lThanhVien getThanhVien(DataTable tv)
    {
        lThanhVien thanhvien = new lThanhVien();
        foreach (DataRow row in tv.Rows)
        {
            thanhvien.username = row["username"].ToString();
            thanhvien.soTaiKhoan = row["SoTaiKhoan"].ToString();
            thanhvien.diaChi = row["DiaChi"].ToString();
            thanhvien.diemTichLuy = float.Parse(row["diemTichLuy"].ToString());
            thanhvien.hoTen = row["hoten"].ToString();
            thanhvien.ngaySinh = DateTime.Parse(row["NgaySinh"].ToString());
        }
        return thanhvien;
    }
    public float getPrice(DataTable gia)
    {
        float g = 0;
        foreach (DataRow row in gia.Rows)
        {
            g = float.Parse(row["GiaTien"].ToString());
        }
        return g;
    }
    public int getCount(DataTable dem)
    {
        int d = 0;
        foreach (DataRow row in dem.Rows)
        {
            d = int.Parse(row["DEM"].ToString());
        }
        return d;
    }
    protected void DangRaoVat(object sender, EventArgs e)
    {
        lThanhVien thanhvien = new lThanhVien();
        string tieuDe = txtTieuDe.Text;
        string noiDung = txtGreeting.Text;
        int loaiRaoVat = int.Parse(cbLoai.SelectedValue);
        string mashop = field.Text;
        // cho nay can phai xet diem tich luy cua thanh vien so voi muc gia cua tin dang(dua vao session)
        string username = Session["user"].ToString();
        string sql = "SELECT * FROM THANHVIEN WHERE username = @username";
        DataTable tv = Database.GetData(sql, "@username", username);
        thanhvien = this.getThanhVien(tv);
        //get price
        string sqlGia = "SELECT GiaTien FROM BANGGIARAOVAT WHERE LOAIRAOVAT = @loairaovat";
        DataTable gia = Database.GetData(sqlGia, "@loairaovat", loaiRaoVat);

        float g = this.getPrice(gia);

        if (thanhvien.diemTichLuy < g)
        {
            lblError.Text = "Bạn không thể đăng tin rao vặt loại này,Bạn hãy chọn loại khác có hạng thấp hơn hoặc nộp thêm tiền";
        }
        else
        {
            //string sqlCount = " SELECT count(*) as DEM FROM RaoVat";
            //DataTable dem = Database.GetData(sqlCount);
            //int d = this.getCount(dem);

            //string sqlIn = "INSERT INTO RaoVat(MaRaoVat,username,tieude,noidung,loairaovat) VALUES(@maraovat,@username,@tieude,@noidung,@loairaovat)";
            //Database.ExecuteNonQuery(sqlIn, "@maraovat", "RV" + (d + 1), "@username", thanhvien.username, "@tieude", tieuDe, "@noidung", noiDung, "@loairaovat", loaiRaoVat);

            string sqlRV = "EXEC DBO.SP_ThemRaoVat @username,@tieude,@noidung,@loairaovat";
            Database.ExecuteNonQuery(sqlRV, "@username", username, "@tieude", tieuDe, "@noidung", noiDung, "@loairaovat", loaiRaoVat);

            string sqlUp = "UPDATE THANHVIEN SET DIEMTICHLUY = @DIEMTICHLUY WHERE USERNAME = @USERNAME";
            Database.ExecuteNonQuery(sqlUp, "@DIEMTICHLUY", (thanhvien.diemTichLuy - g), "@USERNAME", thanhvien.username);

            lblError.Text = "Bạn đã đăng tin thành công!";
            Response.Redirect("home.aspx");
        }
        //Response.Redirect("home.aspx");
    }
    public void cbLoai_SelectedIndexChanged(object sender, EventArgs e)
    {
    }
}